/* *
*
*  1、能耗月统计
*  2、楼栋能耗月统计
*/
(function() {
    // 能耗月统计
    var xAxisData = [];
    var seriesData = [];
    // 楼栋能耗统计
    var f_legends = [];
    var f_xAxisData = [];
    var f_series = [];
    getMounthTotalData();  //初始化

    $('#mounthTotalBtns').on('click', 'button', function(){  // 切换tab
        $(this).removeClass('nofocus').addClass('focus').siblings().removeClass('focus').addClass('nofocus');
        if ( $(this).attr('type') === '1' ) {
            $('#energyConsumptionCensus').css('display', 'block');
            $('#energyMounthTotal').css('display','none');
        }else{
            $('#energyConsumptionCensus').css('display', 'none');
            $('#energyMounthTotal').css('display', 'block');
        }
    })

    function getMounthTotalData() {
        $.ajax({
            url: window.base_url + '/ccp/electryShow/showByDays',
            type: 'post',
            dataType: 'json',
            data: {
                token: window.token,
                time: getPreDay(),
                parkId: 9,
                days: 30,
            },
            success: function (res) {
                console.log('------月统计------');
                console.log(res);
                
                if (res.retcode == 200) {
                    var monthTotal = res.data.monthTotal;
                    if (monthTotal) {
                        var xAxis = Object.keys(monthTotal);
                        xAxis.forEach(function (item) {
                            xAxisData.push(item.substring(5, item.length));
                        });
                        seriesData = Object.values(monthTotal);
                    }
                    var mothPartData = res.data.mothPart;
                    if (mothPartData.config && mothPartData.config.length) {
                        var departsments = mothPartData.config;  // 部门列表
                        var daysData = Object.values(mothPartData.data);
                        let daysConfigData = [];
                        // 生成x坐标
                        Object.keys(mothPartData.data).forEach((item) => {
                            f_xAxisData.push(item.substring(5, item.length))
                        });
                        // 生成图标数据
                        departsments.forEach(function(item) {
                            daysConfigData.push({
                                mounth: []
                            });
                            f_legends.push({
                                name: item.title,
                                textStyle: {
                                    // backgroundColor: item.color
                                    color: '#fff'
                                }
                            });
                        })
                        daysConfigData.forEach((item, index) => {
                            daysData.forEach((value, key) => {
                                item.mounth.push(value[index])
                            });
                        })
                        departsments.forEach((item, index) => {
                            f_series.push({
                                name: item.title,
                                showAllSymbol: true,
                                symbol: 'circle',
                                symbolSize: [5, 5],   // 标记点大小
                                data: daysConfigData[index].mounth,
                                type: 'line',
                                lineStyle: {
                                    color: item.color,
                                    width: 2,   // 线条宽度
                                    opacity: 0.5
                                },
                                itemStyle: {
                                    color: item.color
                                },
                                areaStyle: {
                                    // normal: {},
                                    // color: item.color,
                                    opacity: 0.2,
                                    // normal: {
                                    //     color: new echarts.graphic.LinearGradient(
                                    //         0, 0, 0, 1,
                                    //         [
                                    //             { offset: 0, color: 'rgba(43,153,255,0.6)' },
                                    //             { offset: 1, color: 'rgba(43,153,255,0.2)' },
                                    //         ]
                                    //     )
                                    // },
                                },
                            })
                        })
                    }

                }

                drawEcharts(xAxisData, seriesData);  // 能耗月统计
                drawMultiEcharts(f_xAxisData, f_series, f_legends);  // 楼栋能耗月统计
            },
            error: function (err) {
                console.log(err);
                drawEcharts(xAxisData, seriesData);
                drawMultiEcharts(f_xAxisData, f_series, f_legends);
            }
        })
    };
    function drawEcharts(xValues = [], yValues = []) {  // 能耗月统计
        var option = {
            color: ['#2B99FF'],
            tooltip: {
                trigger: 'axis'
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: xValues,
                axisLabel: {
                    textStyle: {
                        color: '#838687',
                        fontSize: '10'
                    },
                    interval: 0,
                    rotate: 40
                }
            },
            yAxis: {
                inverse: false,
                min:15000,
                max:30000,
                axisLabel: {
                    textStyle: {
                        color: '#838687',
                        fontSize: '10'
                    },
                    interval: 0,
                },
                splitLine: {
                    show: true,
                    lineStyle: {
                        color: '#585858'
                    }
                },
            },
            series: [{
                data: yValues,
                type: 'line',
                areaStyle: {
                    normal: {
                        color: new echarts.graphic.LinearGradient(
                            0, 0, 0, 1,
                            [
                                { offset: 0, color: 'rgba(43,153,255,0.6)' },
                                { offset: 1, color: 'rgba(43,153,255,0.2)' },
                            ]
                        )
                    },
                },

            }]
        };
        echarts.init(document.getElementById('energyConsumptionCensus')).setOption(option); 
    }
    function drawMultiEcharts(xValues = [], yValues = [], legends = []) {  // 楼栋能耗月统计
        var option = {
            legend: {
                data: legends,
            },
            tooltip: {
                trigger: 'axis'
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: xValues,
                axisLabel: {
                    textStyle: {
                        color: '#838687',
                        fontSize: '10'
                    },
                    interval: 0,
                    rotate: 40
                }
            },
            yAxis: {
                inverse: false,
                axisLabel: {
                    textStyle: {
                        color: '#838687',
                        fontSize: '10'
                    },
                    interval: 0,
                },
                splitLine: {
                    show: true,
                    lineStyle: {
                        color: '#585858'
                    }
                },
            },
            series: yValues
        };
        echarts.init(document.getElementById('energyMounthTotal')).setOption(option);
    }
    function getPreDay() {  // 获取前一天时间
        let myTime = new Date();
        let year = myTime.getFullYear();
        let month = myTime.getMonth() + 1;
        let day = myTime.getDate();
        let arr = [];
        day = day - 1  // 取昨天日期
        if (day == 0) {
            if ((month - 1) == 1 || (month - 1) == 3 || (month - 1) == 5 || (month - 1) == 7 || (month - 1) == 8 || (month - 1) == 10 || (month - 1) == 12 || (month - 1) == 0) {
                day = 31;
            } else if ((month - 1) == 2) {
                // 闰年 1种是 能被4整除且不能被100整除 2种 能被400整除
                if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
                    day = 28;
                } else {
                    day = 29;
                }
            } else {
                day = 30;
            }
            month = (month - 1);
            if (month == 0) {
                month = 12;
                year = year - 1;
            }
            // console.log(month)
        }
        arr.push(year);
        arr.push(addZero(month));
        arr.push(addZero(day));
        return arr.join('-');
    };
    function addZero(n) {
        if (n < 10) {
            n = '0' + n
        }
        return n
    };   
})()